<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field v-model="userInfo.username"
                 name="username"
                 label="用户名"
                 placeholder="用户名"
                 :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="userInfo.pwd"
                 type="password"
                 name="pwd"
                 label="密码"
                 placeholder="密码"
                 :rules="[{ required: true, message: '请填写密码' }]" />
      <div style="margin: 16px;">
        <van-button round
                    block
                    type="info"
                    native-type="submit">注册</van-button>
      </div>
    </van-form>
    <van-button @click="$router.push('/login')"
                type="primary">去登陆</van-button>
  </div>
</template>

<script>
import { register } from 'api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      userInfo: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      register(params).then(res => {
        if (res.data.code === 0) {
          Toast.success({
            massage: '注册成功',
            duration: 1000,
            onClose: () => {
              this.$router.replace('/login')
            }
          })
        } else {
          Toast.fail('注册失败')
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
